package com.imoresoft.magic.action;

import java.io.UnsupportedEncodingException;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang.StringUtils;

import com.ambitor.grass.sql.dao.SuperDao;
import com.ambitor.grass.sql.dao.impl.SuperDaoImpl;
import com.ambitor.grass.util.data.DataMap;
import com.ambitor.grass.util.data.DatasetList;
import com.ambitor.grass.util.data.IData;
import com.ambitor.grass.util.data.IDataset;
import com.imoresoft.magic.entity.Result;
import com.imoresoft.magic.view.Pager;

public class StyleAction extends JsonBaseAction {

	private static final long serialVersionUID = -1641837048613839690L;

	private transient SuperDao dao = null;

	private transient Map<String, String> userTemplet = new HashMap<String, String>();

	private Result result = new Result();

	public StyleAction() throws SQLException {
		dao = new SuperDaoImpl(pd.getConn());
	}

	@Override
	public Pager getPager() {
		return pager;
	}

	@Override
	public IDataset getDataset() {
		return dataset;
	}

	public Map<String, String> getUserTemplet() {
		return userTemplet;
	}

	public void setUserTemplet(Map<String, String> userTemplet) {
		this.userTemplet = userTemplet;
	}

	public Result getResult() {
		return result;
	}

	public void setResult(Result result) {
		this.result = result;
	}

	@SuppressWarnings("unchecked")
	// 查询用户已有样式
	public String queryStyle() {
		try {
			String userId = pd.getTopUserId();
			if (StringUtils.isNotBlank(userId)) {
				IData param = new DataMap();
				param.put("VUSER_ID", userId);
				IDataset result = dao
						.executeBaseOnSqlSql("TF_M_USER_TEMPLET_STYLE",
								"SEL_STYLE_BY_USERID", param);

				pager.setTotalCount(result.count());
				dataset
						.addAll(result
								.subList(pager.getFirst(), pager.getEnd()));

				logger.info("共" + pager.getTotalCount() + "条记录");
				logger.info("共" + pager.getTotalPages() + "页");
				logger.info("当前请求的是第" + pager.getCurPageNo() + "页");
				logger.info("从过滤后的结果集中取出从" + (pager.getFirst() + 1) + "到"
						+ pager.getEnd() + "共" + dataset.count() + "条记录.");
			}
		} catch (Exception e) {
			result.setCode(-1);
			result.setMsg("样式获取出错！！！");
			logger.error("", e);
		}

		return SUCCESS;
	}

	// 保存样式
	@SuppressWarnings("unchecked")
	public String styleSave() throws UnsupportedEncodingException {
		try {
			SuperDao superDao = new SuperDaoImpl(pd.getConn());
			IData d = new DataMap(userTemplet);
			d.put("USER_ID", pd.getTopUserId());
			d.put("NICK", pd.getTopNick());
			superDao.insert("TF_M_USER_TEMPLET_STYLE", d);
		} catch (SQLException e) {
			result.setCode(-1);
			result.setMsg("样式保存出错，请重新保存！！！");
			logger.error("", e);
		} catch (Exception e) {
			result.setCode(-1);
			result.setMsg("样式保存出错，请重新保存！！！");
			logger.error("", e);
		}
		return SUCCESS;
	}

	@SuppressWarnings("unchecked")
	// 保存样式
	public String styleDelete() throws UnsupportedEncodingException {
		String styleIds = pd.getRequestParameter("styleIds");
		if (StringUtils.isBlank(styleIds)) {
			result.setCode(-1);
			result.setMsg("请在下表中选择要删除的样式");
			return SUCCESS;
		}

		String[] styleIdArray = styleIds.split(",");
		IDataset paramList = new DatasetList();
		for (String styleId : styleIdArray) {
			IData param = new DataMap();
			param.put("TEMPLET_STYLE_ID", styleId);
			paramList.add(param);
		}

		try {
			SuperDao superDao = new SuperDaoImpl(pd.getConn());
			superDao.delete("TF_M_USER_TEMPLET_STYLE", paramList);
		} catch (Exception e) {
			logger.warn("", e);
			result.setCode(-1);
			result.setMsg("删除规则的过程中发生错误");
		}

		result.setCode(1);
		return SUCCESS;
	}
}
